package com.canbot.u05.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.attendance.AttendanceSettingSelectActivity;
import com.canbot.u05.activity.reception.ReceptionBaseActivity;
import com.canbot.u05.activity.reception.ReceptionModeSettingActivity;
import com.canbot.u05.activity.training.TrainingListViewActivity;
import com.canbot.u05.activity.vipguide.SiteSettingActivity;
import com.canbot.u05.activity.vipguide.VipGuideActivity;
import com.canbot.u05.bean.guide.LocalPointsBean;
import com.canbot.u05.bean.guide.MapInfo;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 办公模式主入口
 */
public class OfficeEnterActivity extends ReceptionBaseActivity {


//        Bitmap[] bitmap = new Bitmap[FileUtil.getFilesLength(ConstUtils.SD_PathStartrecive, ".png")];
//
//        StringBuffer micPath = new StringBuffer();
//
//        String path_content = "u5_voicereceiveanim";
//
//        private int i;
//
//        private static final int STARTRECOGNIZER = 3;
//
//        int rdImgCount;
//
//        int rgImgCount;
//
//        // 显示动画
//        private ImageView merge;

        private GridView mGridView;

        private ArrayList<LocalPointsBean> siteList;

        public static MapInfo mMapInfo;

        private String currentMapName = "";

        private int[] icon_ids = {R.drawable.icon_men_welcome_selector, R.drawable.icon_vip_selector, R.drawable.icon_pavilion_selector,
                R.drawable.icon_attendance_selector, R.drawable.icon_meeting_selector, R.drawable.icon_remote_selector ,R.drawable.item_selector};

        private String[] names = {"迎宾接待", "贵宾导引", "展厅导览", "门禁考勤", "远程会议", "远程替身" ,"培训主持"};

        private Dialog dialogLoading;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //                setContentView(R.layout.act_office_enter);
                setContentView(R.layout.activity_office_setting);


                mGridView = (GridView) findViewById(R.id.gridView);
//                merge = (ImageView) findViewById(R.id.imgview);
//                rdImgCount = FileUtil.getFilesLength(ConstUtils.SD_UuimagestartPath, ".png");
//                rgImgCount = FileUtil.getFilesLength(ConstUtils.SD_PathStartrecognize, ".png");

                initData();
        }

        public void initData() {
                mGridView.setAdapter(new OfficeSettingAdapter());
                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                itemClick(position);
                        }
                });
        }

        /**
         * GridView item 点击处理
         *
         * @param position gridView position
         */
        private void itemClick(int position) {
                switch (position) {
                        case 0:
                                officeMenWelcome();
                                break;
                        case 1:
                                officevip();
                                break;
                        case 2:
                                officePavilion();
                                break;
                        case 3:
                                officeAttendance();
                                break;
                        case 4:
                                officeMeeting();
                                break;
                        case 5:
                                officeRemote();
                                break;
                        case 6:
                                officeTrainingHotel();
                                break;
                }
        }

        /**
         * 返回
         */
        public void officeBack(View view) {
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                finish();
        }

        /**
         * 迎宾接待
         */
        public void officeMenWelcome() {
                MsgSendUtils.sendStringMsg(MsgType.SEND_PAD_MEETING_START, "handSetting");
                Intent intent = new Intent(this, ReceptionModeSettingActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }

        /**
         * 贵宾导引
         */
        public void officevip() {

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                dialogLoading.show();
                final String receptionRoute=SPUtils.getString(OfficeEnterActivity.this,"receptionRoute",null);

                Logger.e("123",receptionRoute);

                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 8 + "");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {


                        @Override
                        public void onSuccess(String result) {

                                if (!TextUtils.isEmpty(result)) {
                                        Type gsonType = new TypeToken<ArrayList<LocalPointsBean>>() {
                                        }.getType();
                                        siteList = new Gson().fromJson(result, gsonType);
                                        for (int i = 0; i < siteList.size(); i++) {
                                                if (siteList.get(i).getPointName().equals(receptionRoute)) {
                                                        cancelDialog();
                                                        startActivity(new Intent(OfficeEnterActivity.this, VipGuideActivity.class)
                                                                .putExtra("isdialog", "isdialog")
                                                                .putExtra("receptionRoute", receptionRoute)
                                                        );
                                                        return;
                                                }
                                        }
                                        cancelDialog();
                                        startActivity(new Intent(OfficeEnterActivity.this, SiteSettingActivity.class)
                                                .putExtra("isFirst", true));
                                }else {
                                        cancelDialog();
                                        startActivity(new Intent(OfficeEnterActivity.this,VipGuideActivity.class));
                                }

                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(OfficeEnterActivity.this).showText(getString(R.string.network_error));

                                        }
                                });

                        }
                });

        }

        private void cancelDialog(){
                runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                                if (dialogLoading != null) {
                                        if (dialogLoading.isShowing()) {
                                                dialogLoading.cancel();
                                        }
                                }
                        }
                });
        }

        /**
         * 展厅导览
         */
        public void officePavilion() {
        }

        /**
         * 门禁考勤
         */
        public void officeAttendance() {
                startActivity(new Intent(OfficeEnterActivity.this, AttendanceSettingSelectActivity.class));
        }

        /**
         * 远程会议
         */
        public void officeMeeting() {
        }

        /**
         * 远程替身
         */
        public void officeRemote() {
        }

        /**
         * 培训主持
         */
        public void officeTrainingHotel() {
                startActivity(new Intent(OfficeEnterActivity.this, TrainingListViewActivity.class));
        }
//        /**
//         * 添加动画
//         */
//        private Handler mHandler = new Handler() {
//                public void handleMessage(android.os.Message msg) {
//                        setAnimeImage(ConstUtils.SD_PathStartrecive, STARTRECOGNIZER, 60);
//                }
//        };
//
//        /**
//         * 设置动画图片
//         */
//        private void setAnimeImage(String imgDir, int stateImage, long time) {
//                Log.e("setAnimeImage", "setAnimeImage" + i);
//                if (bitmap != null && merge != null) {
//                        i++;
//                        if (i >= bitmap.length) {
//                                i = 0;
//                        }
//                        // if (i < bitmap.length) {
//                        if (micPath != null && micPath.length() > 0) {
//                                micPath.delete(0, micPath.length());
//                        }
//                        micPath.append(path_content).append(i).append(".png");
//                        bitmap[i] = Utils.fromSD(imgDir, micPath.toString());
//                        micPath.delete(0, micPath.length());
//                        if (bitmap[i] != null) {
//                                merge.setImageBitmap(bitmap[i]);
//                                Logger.d("TAG", "Bitmap[i]" + bitmap[i].toString());
//                        }
//                        else {
//                                Logger.d("TAG", "Bitmap[i]---null");
//                        }
//                        if (i > 1) {
//                                if (null != bitmap[(i - 1)]) {
//                                        bitmap[(i - 1)].recycle();
//                                        bitmap[(i - 1)] = null;
//                                }
//                        }
//                        mHandler.sendEmptyMessageDelayed(0, time);
//                }
//        }


        @Override
        protected void onResume() {
                super.onResume();
//                setAnimeImage(ConstUtils.SD_PathStartrecive, 2, 60);
                MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "14");
        }

        @Override
        protected void onPause() {
//                mHandler.removeMessages(0);
                cancelDialog();
                super.onPause();
        }

        private class OfficeSettingAdapter extends BaseAdapter {

                @Override
                public int getCount() {
                        return icon_ids.length;
                }

                @Override
                public Object getItem(int position) {
                        return position;
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder mHolder;
                        if (convertView == null) {
                                mHolder = new ViewHolder();
                                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_office_setting, parent, false);
                                mHolder.btn = (Button) convertView.findViewById(R.id.btn);
                                mHolder.name = (TextView) convertView.findViewById(R.id.name);
                                convertView.setTag(mHolder);
                        }
                        else {
                                mHolder = (ViewHolder) convertView.getTag();
                        }

                        mHolder.btn.setBackgroundResource(icon_ids[position]);
                        mHolder.name.setText(names[position]);

                        return convertView;
                }

        }

        static class ViewHolder {

                private Button btn;

                private TextView name;
        }

}
